<?php
/*
 * 2021/7/15
 * 11:43
 * User: 2650332702@qq.com
 */

namespace App\Api\Controllers;


use App\Api\Manages\CommonController;
use App\Api\Repositories\SeckillRepositorie;
use App\Api\Services\JsonService;
use Illuminate\Http\Request;


class SeckillController extends CommonController
{

    //秒杀列表
    public function seckillList()
    {
        $allow = [
            'id',
            'seckill_name',
            'seckill_info',
            'images',
            'price',
            'status',
            'start_time',
            'end_time',
            'stock',
            'paid_num'
        ];
        $seckill = SeckillRepositorie::seckillAll($allow,'start_time');
        if(empty($seckill)){
            return JsonService::success('查询数据为空','',config('code.HTTP_NULL_DATA'));
        }else{
            return JsonService::success('查询成功',$seckill,config('code.HTTP_OK'));
        }
    }
    //时间区间查询
    public function skTime(Request $request)
    {
        $validat = $request->validate([
            'start_time' => 'required',
        ]);
        $allow = [
            'id',
            'seckill_name',
            'seckill_info',
            'images',
            'price',
            'status',
            'start_time',
            'end_time',
            'stock',
            'paid_num'
        ];
        $time = $validat['start_time'];
        $nowday =  SeckillRepositorie::getTimeSk($time,$allow);
        if(!empty($nowday)){
            return JsonService::success('查询成功',$nowday,config('code.HTTP_OK'));
        }else{
            return JsonService::success('查询为空','',config('code.HTTP_NULL_DATA'));
        }
    }

    //实时热抢
    public function hotRush()
    {
        $allowField = [
            'id',
            'seckill_name',
            'seckill_info',
            'images',
            'price',
            'status',
            'start_time',
            'end_time',
            'stock',
            'paid_num'
        ];
        $hotlist = SeckillRepositorie::hotRush($allowField,'paid_num');
        if($hotlist){
            return JsonService::success('查询成功',$hotlist,config('code.HTTP_OK'));
        }else{
            return JsonService::success('查询为空','',config('code.HTTP_NULL_DATA'));
        }
    }

    //商品详情
    public function getDetail(Request $request)
    {

        $validat = $request->validate([
            'seckill_id' => ['required'],
        ]);
        $allowField = [
            'goods_image',
            'goods_introduction',
            'price',
            'goods_content',
            'evaluate_id'
        ];
        $seckillId = $validat['seckill_id'];
        $seckill = SeckillRepositorie::OnceSeckill($seckillId,$allowField);
        $comments = SeckillRepositorie::getComments($seckill['evaluate_id']);
        return JsonService::success(['seckill'=>$seckill,'comments'=>$comments]);
    }
    //进行秒杀
    public function makeSecond(Request $request)
    {
        $validat = $request->validate([
            'uid' => ['required'],
            'seckill_id' => ['required'],
        ]);
        $decr =  SeckillRepositorie::goSeckill((int)$validat['uid'],(int)$validat['seckill_id']);
        if($decr){
            return JsonService::success('秒杀成功,生成订单','',config('code.HTTP_OK'));
        }else{
            return JsonService::fail('秒杀失败','',config('code.HTTP_NOT_FOUND'));
        }
    }

}























